<?php

namespace App\Admin\Controllers;

use App\Admin\Handers\WeChatHander;
use App\Models\Distributor;
use App\Models\DistributorApply;
use App\Models\DistributorLevel;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;

class DistributorApplyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '分销员申请';
	//	状态 1.审核中、2.审核成功、3.审核不通过
	protected $status = [1 => '审核中', 2 => '审核成功', 3 => '审核不通过'];
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DistributorApply());
        
        $grid->column('name', __('Name'));
        $grid->column('mobile', __('Mobile'));
        $grid->column('status', __('Status'))->using($this->status)
	        ->dot([1 => 'info', 2 => 'success', 3 => 'danger']);
        $grid->column('created_at', __('Created at'));
        
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
        	$filter->disableIdFilter();
        	$filter->like('name', __('Name'));
        	$filter->like('mobile', __('Mobile'));
        	$filter->equal('status', __('Status'))->radio(Arr::add($this->status, '', '全部'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DistributorApply::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('mobile', __('Mobile'));
        $show->field('user_id', __('User id'));
        $show->field('pid', __('Pid'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DistributorApply());

        $form->text('name', __('Name'));
        $form->mobile('mobile', __('Mobile'));
        $form->hidden('user_id');
        $form->text('user.nick_name', '微信用户名')->disable();
        
        $form->display('pid', __('Pid'))->with(function ($data) {
            $user = User::find($data);
            return $user->nick_name;
        });
        
        $form->select('distributor_level_id', __('Distributor level id'))
	        ->options([1 => '分销员'])
	        ->default(1)
            ->required();

        $form->myradio('status', __('Status'))->options(Arr::except($this->status, '1'));
		
        $form->ignore(['distributor_level_id']);
        $form->saved(function ($form) {
        	// 审核通过后，创建新的分销员数据，user表关联创建的数据
            if($form->status == 2) {
				$wechart = new WeChatHander();
                $distributor = new Distributor();
                $distributor->name = $form->model()->name;
                $distributor->mobile = $form->model()->mobile;
                $distributor->distributor_level_id = \request('distributor_level_id');
                $distributor->promo_code_url = $wechart->getPromoCodeUrl($form->user_id);
                $distributor->save();
                $distributor->promo_code = promoCode($distributor->id);
                $distributor->save();
                
                $user = User::find($form->user_id);
                $user->distributor_id = $distributor->id;
                $user->save();
                
            }
        });
        return $form;
    }
}
